﻿Public Class Main

	Private Sub btnInputSelect_Click(sender As System.Object, e As System.EventArgs) Handles btnInputSelect.Click
		Dim OFD As New OpenFileDialog
		If (OFD.ShowDialog() = Windows.Forms.DialogResult.OK) Then
			txtInputFile.Text = OFD.FileName
		End If
	End Sub

	Private Sub btnOutputSelect_Click(sender As System.Object, e As System.EventArgs) Handles btnOutputSelect.Click
		Dim FBD As New FolderBrowserDialog
		If (FBD.ShowDialog() = Windows.Forms.DialogResult.OK) Then
			txtOutputFolder.Text = FBD.SelectedPath
		End If
	End Sub

	Private Sub btnConvert_Click(sender As System.Object, e As System.EventArgs) Handles btnConvert.Click
		Convert()
	End Sub

    Private Function ByteSwap(Bytes As Byte())
        Dim Index As Integer
        Dim Length As Integer = Bytes.Length - 2
        Dim Result(Bytes.Length - 1) As Byte
        Dim SwapByte As Byte

        Array.Copy(Bytes, Result, Bytes.Length)

        For Index = 0 To Length Step 2
            SwapByte = Result(Index)
            Result(Index) = Result(Index + 1)
            Result(Index + 1) = SwapByte
        Next

        Return Result
    End Function

    Private Sub Convert()
        Dim FileBytes As Byte() = My.Computer.FileSystem.ReadAllBytes(txtInputFile.Text)
        Dim FileStream As New IO.MemoryStream(FileBytes)
        Dim OutputFileName As String
		Dim OutputFileIndex As Integer = 0
		Dim DecodedStream As IO.MemoryStream
		Dim OutputStream As IO.MemoryStream

        If (radioByteOrderSwapped.Checked) Then
            FileBytes = ByteSwap(FileBytes)
        End If

		Dim Encoder As CodecInterface.ICodec
		Dim Decoder As CodecInterface.ICodec = GetInputCodec()

		Do
			DecodedStream = New IO.MemoryStream()
			If (Decoder.Decode(FileStream, DecodedStream)) Then

				Encoder = GetOutputCodec()
				OutputFileName = OutputFileIndex.ToString
				Do While (OutputFileName.Length < 3)
					OutputFileName = "0" & OutputFileName
				Loop
				OutputFileName = My.Computer.FileSystem.CombinePath(txtOutputFolder.Text, OutputFileName & ".wav")
				OutputFileIndex += 1

				OutputStream = New IO.MemoryStream()
				DecodedStream.Position = 0
				Encoder("samplerate") = Decoder("samplerate")
				Encoder("bitspersample") = Decoder("bitspersample")
				Encoder("channels") = Decoder("channels")
				Encoder("loopstart") = Decoder("loopstart")
				Encoder("loopenabled") = Decoder("loopenabled")
				Encoder("looplength") = Decoder("looplength")
				Encoder.Encode(DecodedStream, OutputStream)
				My.Computer.FileSystem.WriteAllBytes(OutputFileName, OutputStream.ToArray, False)

				DecodedStream.Dispose()
				OutputStream.Dispose()
			Else
				DecodedStream.Dispose()
				Exit Do
			End If
		Loop

        MsgBox("Conversion complete.", MsgBoxStyle.Information)
	End Sub

	Private Function GetInputCodec() As CodecInterface.ICodec
		Dim Result As CodecInterface.ICodec = Nothing

		Select Case (TabControl1.SelectedTab.Text)
			Case "K054539"
				Result = GetCodec(CodecList.K054539)
				Result("dpcmtable") = IIf(radioDPCMSquares.Checked, "1", "0")
				Result("swapnybble") = IIf(radioNybbleOrderSwapped.Checked, "True", "False")
			Case "OKI6295"
				Result = GetCodec(CodecList.OKI6295)
			Case "WAV"
				Result = GetCodec(CodecList.WAV)
		End Select

		Return Result
	End Function

	Private Function GetOutputCodec() As CodecInterface.ICodec
		Return GetCodec(CodecList.WAV)
	End Function
End Class
